﻿using BLL;
using Models;
using Inspinia_MVC5.Code;
using Inspinia_MVC5.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Inspinia_MVC5.Controllers
{
    [AllowCors]
    [SessionTimeOutFilter]
    public class IndicatorController : Controller
    {
        // GET: Indicator
        public JsonResult Index(string blockid, int page = 1, int size = 12)
        {
            int num = 3;
            string blockwellstr = "";
            WellService WellService = new WellService();
            var wells = WellService.All();
            List<IndicatorModel> IndicatorModel = new List<IndicatorModel>();
            if (Session["identity"].ToString() == null)
            {
                return Json(new { status = "7" }, JsonRequestBehavior.AllowGet);
            }
            else if (Session["identity"].ToString() == "Admin")
            {
                if (blockid != "0")
                {
                    wells = wells.Where(c =>c.BLOCK_ID.ToString() == blockid);
                }
            }
            else
            {
                if (blockid == "0")
                {
                    string wellstr = Session["wellstr"].ToString();
                    wells = wells.Where(c => wellstr.Contains("-" + c.ID.ToString() + "-"));
                }
                else
                {
                    blockwellstr = getBlockWell(blockid);
                    wells = wells.Where(c => blockwellstr.Contains("-" + c.ID.ToString() + "-"));
                }
            }
            foreach (var well in wells)
            {
                List<IndicatorModel> inddmodel = new List<IndicatorModel>();
                inddmodel = ResultHelper.getIndicatorsByNum(well.ID.ToString(),well.Name1, num);
                IndicatorModel = IndicatorModel.Union(inddmodel).ToList();
            }
            if (IndicatorModel.Count() == 0)
                return Json(new { status = "3" }, JsonRequestBehavior.AllowGet);
            return Json(new { status = "0", totalnum = IndicatorModel.Count(), data = IndicatorModel.Skip((page - 1) * size).Take(size) }, JsonRequestBehavior.AllowGet);
        }
        public JsonResult History(string wellid, DateTime starttime, DateTime endtime, int page = 1, int size = 12)
        {
            string wellstr = Session["wellstr"].ToString();
            if (wellstr=="0"||wellstr.Contains("-" + wellid + "-"))
            {
                List<IndicatorModel> IndicatorModel = new List<IndicatorModel>();
                IndicatorModel = ResultHelper.getIndicatorsByTime(wellid, starttime, endtime);
                if (IndicatorModel.Count() == 0)
                    return Json(new { status = "3" }, JsonRequestBehavior.AllowGet);
                return Json(new { status = "0", totalnum = IndicatorModel.Count(), data = IndicatorModel.Skip((page - 1) * size).Take(size) }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { status = "2" }, JsonRequestBehavior.AllowGet);
            }
        }
        public JsonResult AllHistory(string wellid, DateTime starttime, DateTime endtime, string type)
        {
            string wellstr = Session["wellstr"].ToString();
            if (wellstr == "0" || wellstr.Contains("-" + wellid + "-"))
            {
                if(type == "12")
                {
                    R_SIGN_INDD_12Service R_SIGN_INDD_12Service = new R_SIGN_INDD_12Service();
                    List<R_SIGN_INDD_12> R_SIGN_INDD_12 = R_SIGN_INDD_12Service.FindAll(c => c.Well_ID == wellid && c.Time >= starttime && c.Time <= endtime).OrderByDescending(c => c.Time).ToList();
                    if (R_SIGN_INDD_12.Count() == 0)
                        return Json(new { status = "3" }, JsonRequestBehavior.AllowGet);
                    return Json(new { status = "0", data = R_SIGN_INDD_12 }, JsonRequestBehavior.AllowGet);
                }
                else if (type == "24")
                {
                    R_SIGN_INDD_24Service R_SIGN_INDD_24Service = new R_SIGN_INDD_24Service();
                    List<R_SIGN_INDD_24> R_SIGN_INDD_24 = R_SIGN_INDD_24Service.FindAll(c => c.Well_ID == wellid && c.Time >= starttime && c.Time <= endtime).OrderByDescending(c => c.Time).ToList();
                    if (R_SIGN_INDD_24.Count() == 0)
                        return Json(new { status = "3" }, JsonRequestBehavior.AllowGet);
                    return Json(new { status = "0", data = R_SIGN_INDD_24 }, JsonRequestBehavior.AllowGet);
                }
                else if (type == "30")
                {
                    R_SIGN_INDD_30Service R_SIGN_INDD_30Service = new R_SIGN_INDD_30Service();
                    List<R_SIGN_INDD_30> R_SIGN_INDD_30 = R_SIGN_INDD_30Service.FindAll(c => c.Well_ID == wellid && c.Time >= starttime && c.Time <= endtime).OrderByDescending(c => c.Time).ToList();
                    if (R_SIGN_INDD_30.Count() == 0)
                        return Json(new { status = "3" }, JsonRequestBehavior.AllowGet);
                    return Json(new { status = "0", data = R_SIGN_INDD_30 }, JsonRequestBehavior.AllowGet);
                }
                else if (type == "0")
                {
                    R_SIGN_INDD_Service R_SIGN_INDD_Service = new R_SIGN_INDD_Service();
                    List<R_SIGN_INDD_> R_SIGN_INDD_ = R_SIGN_INDD_Service.FindAll(c => c.Well_ID == wellid && c.Time >= starttime && c.Time <= endtime).OrderByDescending(c => c.Time).ToList();
                    if (R_SIGN_INDD_.Count() == 0)
                        return Json(new { status = "3" }, JsonRequestBehavior.AllowGet);
                    return Json(new { status = "0", data = R_SIGN_INDD_ }, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    R_SIGN_INDDService R_SIGN_INDDService = new R_SIGN_INDDService();
                    List<R_SIGN_INDD> R_SIGN_INDD = R_SIGN_INDDService.FindAll(c => c.Well_ID == wellid && c.Time >= starttime && c.Time <= endtime).OrderByDescending(c => c.Time).ToList();
                    if (R_SIGN_INDD.Count() == 0)
                        return Json(new { status = "3" }, JsonRequestBehavior.AllowGet);
                    return Json(new { status = "0", data = R_SIGN_INDD }, JsonRequestBehavior.AllowGet);
                }
            }
            else
            {
                return Json(new { status = "2" }, JsonRequestBehavior.AllowGet);
            }
        }
        //获取当前区块下所拥有井ID字符串
        public string getBlockWell(string blockid)
        {
            string blockwellstr = "";
            string wellstr = Session["wellstr"].ToString();
            WellService WellService = new WellService();
            var wells = WellService.All().Where(c => c.BLOCK_ID.ToString() == blockid && wellstr.Contains("-" + c.ID.ToString() + "-"));
            blockwellstr += "-";
            foreach (var well in wells)
            {
                blockwellstr += well.ID;
                blockwellstr += "-";
            }
            return blockwellstr;
        }
        public ActionResult BlockIndicator()
        {
            return View();
        }
        public ActionResult IndicatorHistory()
        {
            return View();
        }
        public ActionResult IndicatorCompare()
        {
            return View();
        }
    }
}